#!/usr/bin/env bash
# shellcheck disable=SC2257

failed=0
fail() {
  echo "$(tput setaf 1)$*$(tput sgr0)"
  failed=1
}

i=1 j=1
cat << EOF

Manual Checklist

$((i++)). Make sure README.md examples are up to date
$((i++)). Format and read over the manual for bad formatting and outdated info.
$((i++)). Run \`builders/build_builder build/*/\` to update all builder images.
$((i++)). \`builders/run_builder dist-newstyle/sdist/ShellCheck-*.tar.gz builders/*/\` to verify that they work.
$((i++)). \`for f in \$(cat build/*/tag); do docker push "\$f"; done\` to upload them.
$((i++)). Run test/distrotest to ensure that most distros can build OOTB.
$((i++)). Make sure GitHub Build currently passes: https://github.com/koalaman/shellcheck/actions
$((i++)). Make sure SnapCraft build currently works: https://snapcraft.io/shellcheck/builds
$((i++)). Make sure the Hackage package builds locally.
$((i++)). Make sure none of the automated checks below fail

Release Steps

$((j++)). \`cabal sdist\` to generate a Hackage package
$((j++)). \`git push --follow-tags\` to push commit
$((j++)). Wait for GitHub Actions to build. (v0.11.0 "Deploy" failed, but worked on retry)
$((j++)). Verify release:
  a. Check that the new versions are uploaded: https://github.com/koalaman/shellcheck/tags
  b. Check that the docker images have version tags: https://hub.docker.com/u/koalaman
$((j++)). If no disaster, upload to Hackage: http://hackage.haskell.org/upload
$((j++)). Run 'autoupdate' from https://github.com/koalaman/shellcheck-precommit
$((j++)). Release new snap versions on https://snapcraft.io/shellcheck/releases
$((j++)). Push a new commit that updates CHANGELOG.md

Automated Checks

EOF

if git diff | grep -q ""
then
  fail "There are uncommitted changes"
fi

if [[ $(git log -1 --pretty=%B) != *"CHANGELOG"* ]]
then
  fail "Expected git log message to contain CHANGELOG"
fi

version=${current#v}
if ! grep "Version:" ShellCheck.cabal | grep -qFw "$version"
then
  fail "The cabal file does not match tag version $version"
fi

if ! grep -qF "## $current" CHANGELOG.md
then
  fail "CHANGELOG.md does not contain '## $current'"
fi

current=$(git tag --points-at)
if [[ -z "$current" ]]
then
  fail "No git tag on the current commit"
  echo "Create one with:   git tag -a v0.0.0"
fi

if [[ "$current" != v* ]]
then
  fail "Bad tag format: expected v0.0.0"
fi

if [[ "$(git cat-file -t "$current")" != "tag" ]]
then
  fail "Current tag is not annotated (required for Snap)."
fi

if [[ "$(git tag --points-at master)" != "$current" ]]
then
  fail "You are not on master"
fi

if [[ $(git log -1 --pretty=%B) != "Stable version "* ]]
then
  fail "Expected git log message to be 'Stable version ...'"
fi
exit "$failed"
